// A very simple Binary Tree Example of Inserting and Displaying
// By Ben 21:48 25/09/2016

#include <iostream>

using namespace std;
using std::cout;
using std::endl;

typedef struct BTreeNode
{
	struct BTreeNode *Left;
	struct BTreeNode *Right;
	int data;
}TreeNode;

static TreeNode *root = NULL;

TreeNode *NewNode(int iData){
	TreeNode *tn = new TreeNode();
	//Add a new node
	tn->Left = NULL;
	tn->Right = NULL;
	tn->data = iData;
	return tn;
}

TreeNode *Insert(TreeNode *node, int iData){
	if (node == NULL){
		return NewNode(iData);
	}

	if (iData <= node->data){
		if (node->Left == NULL){
			node->Left = NewNode(iData);
		}
		else{
			node->Left = Insert(node->Left, iData);
		}
	}
	else{
		if (node->Right == NULL){
			node->Right = NewNode(iData);
		}
		else{
			node->Right = Insert(node->Right, iData);
		}
	}
	return node;
}


void PrintTree(TreeNode *node){
	if (node == NULL){
		return;
	}
	//Get left side.
	PrintTree(node->Left);
	//Display the data in the tree
	cout << node->data << endl;
	//Get right side
	PrintTree(node->Right);
}

int main(int argc, char *argv[]){
	
	int  i = 1;
	//Add the first node
	root = Insert(NULL, 10);

	while (i < 12){
		i++;
		//Insert a new node
		root = Insert(root, (i * 10));
	}

	//Display the tree.
	PrintTree(root);

	system("pause");
	return 0;


}